package com.example.demo.controller;

import com.example.demo.bean.OrdersVo;
import com.example.demo.bean.ResultPageVO;
import com.example.demo.bean.ResultVO;

import com.example.demo.requestentity.OrdersRequestVO;
import com.example.demo.service.impl.OrdersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@RequestMapping("api/order")
@RestController
@CrossOrigin
public class OrderController {

    @Autowired
    public OrdersServiceImpl ordersService;

    /**
     * 饮料查询
     * https://api.xuliangzhan.com:10443/demo/api/pub/page/list/15/1?name=d
     * {
     *     "page":{
     *         "pageSize":15,
     *         "currentPage":1,
     *         "total":244
     *     },
     *     "result":[
     *         {
     *             "id":853,
     *             "name":"dfgfdg6",
     *             "email":"hdfgdf698@163.com",
     *             "nickname":"dfgfdg6",
     *         }
     *     ]
     * }
     */
    @GetMapping("/list/{pageSize}/{currentPage}")
    public ResultPageVO<OrdersVo> query(@RequestParam(required = false , value = "name" , defaultValue = "") String name ,
                                        @PathVariable(value = "pageSize") Integer pageSize ,
                                        @PathVariable(value = "currentPage") Integer currentPage){
        return ordersService.query(name , pageSize , currentPage);
    }


    /**
     * 
     * @param ordersRequestVO
     * @return 
     * @author kongjiawei
     * @date 2021/9/5 16:23
     */
    @PostMapping("/operation")
    public ResultVO operation(@RequestBody OrdersRequestVO ordersRequestVO){
        //Todo
        return ordersService.operation(ordersRequestVO);
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response){
        //生产订单表
        ordersService.export();
        String path = "E:\\test\\output\\output.xlsx";
        File file = new File(path);
        String fileName = null;
        try {
            fileName = URLEncoder.encode("订单表.xlsx", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setContentType("application/x-download");
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
        response.setCharacterEncoding("UTF-8");
        try {
            InputStream stream = new FileInputStream(file);

            ServletOutputStream out = response.getOutputStream();

            byte buff[] = new byte[1024];

            int length = 0;

            while ((length = stream.read(buff)) > 0) {
                out.write(buff,0,length);

            }

            stream.close();

            out.close();

            out.flush();

        } catch (IOException e) {
            e.printStackTrace();

        }

    }
}
